<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据表格</title>
    <link rel="stylesheet" href="Layuimini/lib/layui-v2.6.3/css/layui.css" th:href="@{Layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" href="Layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css" th:href="@{Layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <link rel="stylesheet" href="Layuimini/css/public.css" th:href="@{Layuimini/css/public.css}" media="all">
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="superName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="superMobile" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" name="superEmail" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="superAddress" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" id="searchBtn" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>

                            <button type="button" class="layui-btn layui-btn-primary" id="resetBtn">
                                <i class="fa fa-refresh"></i> 重 置
                            </button>

                            <button  type="button" style="margin-left: 30px" id="excmodel" class="layui-btn layui-btn-primary">
                                <i class="fa fa-file-o"></i>
                                Excel模板导出
                                <!-- <i class="layui-icon">&#xe601;</i>-->
                            </button>

                            <button  type="button" id="excBtn" class="layui-btn layui-btn-primary">
                                <i class="fa fa-download"></i>
                                数据导出
                               <!-- <i class="layui-icon">&#xe601;</i>-->
                            </button>

                            <button  type="button" id="uploadExample" class="layui-btn layui-btn-primary">
                                <i class="fa fa-upload"></i>
                                数据导入
                                <!--<i class="layui-icon">&#xe681;</i>-->
                            </button>


                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
                <button class="layui-btn layui-btn-sm layui-btn-warm data-delete-btn" lay-event="resetTable"> <i class="fa fa-refresh"></i>刷新</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script  src="jQuery/jquery-2.1.1.min.js" th:src="@{LightYear/js/jquery.min.js}" charset="utf-8"></script>
<script  src="Layuimini/lib/layui-v2.6.3/layui.js" th:src="@{Layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: 'superTable/getLayuiSearch',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'superName', width: 120, title: '姓名'},
                    {field: 'superSex', width: 60, title: '性别',templet: sexFormat},
                    {field: 'superAge', width: 80, title: '年龄', sort: true},
                    {field: 'superMobile', width: 120,title: '手机',align: 'center'},
                    {field: 'superEmail', width: 220, title: '邮箱',align: 'center',hide:true},
                    {field: 'superAddress', width: 220, title: '地址',align: 'center'},
                    {field: 'superSalary', width: 125, title: '薪资', sort: true,hide:true},
                    {field: 'superIdCard', width: 180, title: '身份证',align: 'center',hide:true},
                    {field: 'superZodiac', width: 60, title: '生肖'},
                    {field: 'constellation', width: 100, title: '星座'},
                    //{field: 'createTime', width: 80, title: '创建时间',hide:true},
                    {title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            page: {
            limit:5,
                limits:[5,10,20,30,40,50,9999],
                groups: 3
        }, //开启分页
            skin: 'line'
        });

        // 格式化显示男女信息
        function sexFormat(d) {
            var str;
            if (d.superSex == "男") {
                str = '男';
            } else if (d.superSex == "女") {
                str = '女';
            } else {
                str = '<span class="fontColor_red">未知</span>';
            }
            return str;
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
         // console.log(result);
            if(data.field.superName != '' )
                var name = data.field.superName;
            if(data.field.superMobile != '' )
                var iphone = data.field.superMobile;
            if(data.field.superEmail != '' )
                var email = data.field.superEmail;
            if(data.field.superAddress != '' )
                var address = data.field.superAddress;


            //执行搜索重载
            table.reload('currentTableId', {
                url: 'superTable/queryUserByOther',
                method: 'POST',
                page: {
                    curr: 1
                }
                , where: {
                    superName:name,
                    superMobile:iphone,
                    superEmail:email,
                    superAddress:address
                   // searchParams: resultData
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'toAddPage',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听批量删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(checkStatus.data.length==0){
                    layer.msg("请至少选择一行数据！");
                    return false;
                }
                var infoNames = new Array();
                var ids =  new Array();

                for (var i = 0; i < checkStatus.data.length; i++) {
                    infoNames.push(checkStatus.data[i].superName);
                }
                for (var i = 0; i < checkStatus.data.length; i++) {
                    ids.push(checkStatus.data[i].id);
                }
                var strname = infoNames.join(",");

                layer.confirm('确定要删除['+strname+']吗？',{btn:['确定','取消']},function(){
                    $.ajax({
                        url:"superTable/delInfoAll?ids="+ids,
                        type:"DELETE",
                        success:function(){
                            layer.msg('删除'+checkStatus.data.length+'条数据成功！',{
                                time:2000
                            },function(){
                                //window.location.reload();
                                //layer.msg("删除"+checkStatus.data.length+"条数据成功！")
                                table.reload('currentTableId',{});  //初始化表格
                            })
                        }
                    })

                })


                //layer.alert(JSON.stringify(data));
            }else if(obj.event === 'resetTable'){
               // table.reload('currentTableId',{});
               // window.parent.location.reload();
                window.location.reload();
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = data.id;  //字段id
            var name = data.superName;  //字段姓名

            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'toEditPage?id='+id
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除['+name+']吗？', function (index) {
                   // obj.del();
                    $.ajax({
                        url:"superTable/delInfo/"+id,
                        type:"DELETE",
                        success:function(){
                            layer.msg('删除成功！',{
                                time:2000
                            },function(){
                                //window.location.reload();
                                table.reload('currentTableId',{});  //初始化表格
                            })
                        }
                    })


                    layer.close(index);
                });
            }
        });


    });



</script>

<script>


    $("#resetBtn").click(function () {
        $("input[name='superName']").val("");
        $("input[name='superEmail']").val("");
        $("input[name='superMobile']").val("");
        $("input[name='superAddress']").val("");
        layer.msg("重置成功！", {
            time: 1000, //6s后自动关闭
            icon:6
        });
    })

    //模板导出
    $("#excmodel").click(function () {
        layer.confirm('确定要导出Excel模板吗？', function (index) {
            layer.msg('模板导出中...！',{time:2000},function(){
                window.location.href="ExcelModel";
            })
            layer.close(index);
        });
    })



        //数据导出
    $("#excBtn").click(function () {
        layer.confirm('确定要导出Excel数据吗？', function (index) {

            window.location.href="superTable/export";


            layer.close(index);
        });
    })


    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#uploadExample' //绑定元素
            ,url: 'superTable/importInfo' //上传接口
            ,accept: 'file'
            ,exts: 'xls|xlsx|xlsm'
            ,done: function(res){
                layui.use('layer', function(){
                    var layer = layui.layer;

                   // console.log(res)
                    layer.msg(res.message);

                   /* layer.msg(res.url, {
                        time: 6000, //6s后自动关闭
                        icon:1
                    });*/
                });
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>

</body>
</html>